/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package yancy.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import yancy.common.annotation.SysLog;
import yancy.common.utils.PageUtils;
import yancy.common.utils.R;
import yancy.common.validator.ValidatorUtils;
import yancy.common.validator.group.AddGroup;
import yancy.common.validator.group.UpdateGroup;
import yancy.modules.sys.entity.SysBook;
import yancy.modules.sys.entity.SysFile;
import yancy.modules.sys.service.SysBookAuthorService;
import yancy.modules.sys.service.SysBookFileService;
import yancy.modules.sys.service.SysBookService;
import yancy.modules.sys.service.SysFileService;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @book Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/book")
@AllArgsConstructor
public class SysBookController extends AbstractController {
    private final SysBookService SysBookService;
    private final SysBookService bookService;
    private final SysBookAuthorService bookAuthorService;
    private final SysBookFileService bookFileService;
    private final SysFileService fileService;


    /**
     * 所有用户列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //只有超级管理员，才能查看所有管理员列表
//        if (getUserId() != Constant.SUPER_ADMIN) {
//            params.put("createUserId", getUserId());
//        }
        Page page = bookService.query(getPage(params), params);
        PageUtils pageUtils = new PageUtils(page);

        return R.ok().put("page", pageUtils);
    }


    /**
     * 用户信息
     */
    @GetMapping("/info/{bookId}")
    public R info(@PathVariable("bookId") Long bookId) {
        SysBook book = bookService.getById(bookId);

        //获取用户所属的角色列表
        List<Long> bookIdList = bookAuthorService.queryAuthorIdList(bookId);
        List<Long> fileIdList = bookFileService.queryFileIdList(bookId);
        book.setAuthorIdList(bookIdList);
        book.setFileIdList(fileIdList);
        book.setFileList(Collections.emptyList());

        if (CollectionUtils.isNotEmpty(fileIdList)) {
            List<SysFile> files = fileService.list(
                new QueryWrapper<SysFile>().in("file_id", fileIdList)
                    .eq("status", 1));
            book.setFileList(files);
        }


        return R.ok().put("book", book);
    }


    /**
     * 保存用户
     */
    @SysLog("保存图书")
    @PostMapping("/save")
    public R save(@RequestBody SysBook book) {
        ValidatorUtils.validateEntity(book, AddGroup.class);

        book.setCreateUserId(getUserId());
        book.setCreateTime(new Date());
        bookService.saveBook(book);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @PostMapping("/update")

    public R update(@RequestBody SysBook book) {
        ValidatorUtils.validateEntity(book, UpdateGroup.class);
        book.setCreateUserId(getUserId());
        SysBookService.update(book);
        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] bookIds) {
        if (bookIds == null) return R.ok();
        StringBuilder retIds = new StringBuilder();
        for (Long bookId : bookIds) {
            if (!bookService.deleteById(bookId)) {
                retIds.append(bookId).append(",");
            }
        }
        if (retIds.length() != 0) {
            retIds.deleteCharAt(retIds.length()-1);
        }

        return retIds.length() != 0 ? R.error(retIds.toString() + "不能被删除") : R.ok();

    }
}
